
namespace Behemoth.SharePoint.CustomFieldTypes {

    using System;
    using System.Collections.Generic;
    using System.Text;

    using Microsoft.SharePoint;

	public class FlexLookupFieldValueItemCollection : List<FlexLookupFieldValueItem> {

		public FlexLookupFieldValueItemCollection() : base() { }
		public FlexLookupFieldValueItemCollection( string multiColumnFormatString ) : base() {
			multiColumnFormatString = SPFieldMultiColumnValueTools.CanonicalizeMultiColumnFormatString( multiColumnFormatString );
			SPFieldMultiChoiceValue newValue = new SPFieldMultiChoiceValue( multiColumnFormatString );
			for (int i = 0; i < newValue.Count; i += 2) {
				string key = newValue[i];
				string text = newValue[i + 1];
				FlexLookupFieldValueItem item = new FlexLookupFieldValueItem( key, text );
				this.Add( item );
			}
		}

		/// <summary>
		/// Convert the collection to a ;# delimited string.
		/// Provide a version that trims the leading and trailing delimiters.
		/// </summary>
		/// <param name="trimLeadingAndTrailingDelimeter">
		/// If true, leading and trailing delimiters are trimmed. Default is false.
		/// </param>
		/// <returns></returns>
		public override string ToString() {
			SPFieldMultiChoiceValue newValue = new SPFieldMultiChoiceValue();
			foreach (FlexLookupFieldValueItem item in this) {
				newValue.Add( item.Key );
				newValue.Add( item.Text );
			}
			return newValue.ToString();
		}
		public string ToString( bool trimLeadingAndTrailingDelimeter ) {
			return SPFieldMultiColumnValueTools.TrimMultiColumnFormatString( this.ToString(), trimLeadingAndTrailingDelimeter, trimLeadingAndTrailingDelimeter );
		}

		#region Find Methods

		// TODO use a list searching predicate here
		public FlexLookupFieldValueItem FindByKey( string key, bool ignoreCase ) {
			FindFlexLookupFieldValueItem finder = new FindFlexLookupFieldValueItem( key, string.Empty, ignoreCase );
			return this.Find( finder.FlexLookupFieldValueItemPredicate );
		}
		public FlexLookupFieldValueItem FindByKey( string key ) {
			return FindByKey( key, true );
		}

		public FlexLookupFieldValueItem FindByText(string text, bool ignoreCase) {
			FindFlexLookupFieldValueItem finder = new FindFlexLookupFieldValueItem( string.Empty, text, ignoreCase );
			return this.Find( finder.FlexLookupFieldValueItemPredicate );
		}
		public FlexLookupFieldValueItem FindByText( string text ) {
			return FindByText( text, true );
		}

		#endregion

	} // FlexLookupFieldValueItemCollection

	public class FindFlexLookupFieldValueItem {

		private string _key;
		private string _text;
		private bool _ignoreCase;

		public FindFlexLookupFieldValueItem( string key, string text, bool ignoreCase ) {
			this._key = key;
			this._text = text;
			this._ignoreCase = ignoreCase;
		}

		public bool FlexLookupFieldValueItemPredicate( FlexLookupFieldValueItem target ) {
			return (
				(!string.IsNullOrEmpty( this._key ) && string.Compare(target.Key, this._key, this._ignoreCase) == 0)
				|| (!string.IsNullOrEmpty( this._text ) && string.Compare( target.Text, this._text, this._ignoreCase ) == 0)
			);
		}

	} // class FindFlexLookupFieldValueItem

} // namespace
